#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

/**
 * Return an array of size *returnSize.
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* countBits(int num, int* returnSize) {
    *returnSize = num + 1;
    int *res = malloc(*returnSize * sizeof(*res));
    memset(res, 0, *returnSize * sizeof(*res));
    for (int i = 0; i < *returnSize; i++) {
        res[i] = i % 2 + res[i / 2];
    }
    return res;
}

int* countBits1(int num, int* returnSize) {
    *returnSize = num + 1;
    int *res = malloc(*returnSize * sizeof(*res));
    memset(res, 0, *returnSize * sizeof(*res));
    for (int i = 0; i < *returnSize; i++) {
        for (int j = 0; i >> j > 0 && j < 8 * sizeof(*res); j++) {
            res[i] += (i >> j) & 0x0001;
        }
    }
    return res;
}

int main() {
    int num = 8;
    int resSize = 0;
    int *res = countBits1(num, &resSize);
    printf("resSize = %d\n", resSize);
    for (int i = 0; i < resSize; i++) {
        printf("res[%d] = %d\n", i, res[i]);
    }
    free(res);
    return 0;
}